/**
 * 
 */
package ua.ws.feedsreader.actions;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import ua.ws.feedsreader.common.User;
import ua.ws.feedsreader.db.SemanticManager;
import ua.ws.feedsreader.utils.Log;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author ricardo
 *
 */
public class Login extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 751099725010096326L;
	
	//inner params
	private String username;
	private String password;
		
	private String errorLoginMessage;
	
	@Override
	@Action(results={@Result(name="success", location="feeds-reader", type="redirect" )})
	public String execute() throws Exception {
		
		Map<String, Object> session = ActionContext.getContext().getSession();

		SemanticManager semanticManager = new SemanticManager();
		User login = semanticManager.login( username, password );
		
		if( login == null ) {
			setErrorLoginMessage("Login incorrecto");
			return ERROR;
		}
		
		// save user to session
		session.put("USER", login);
		
		Log.debug("login for " + username + " is "+ login );
		
		return SUCCESS;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the errorLoginMessage
	 */
	public String getErrorLoginMessage() {
		return errorLoginMessage;
	}

	/**
	 * @param errorLoginMessage the errorLoginMessage to set
	 */
	public void setErrorLoginMessage(String errorLoginMessage) {
		this.errorLoginMessage = errorLoginMessage;
	}


}
